package androidx.test.espresso.action;

import android.util.Log;
import android.view.View;
import android.view.ViewConfiguration;
import android.webkit.WebView;
import androidx.test.espresso.PerformException;
import androidx.test.espresso.UiController;
import androidx.test.espresso.ViewAction;
import androidx.test.espresso.action.Tapper;
import androidx.test.espresso.core.internal.deps.guava.base.Optional;
import androidx.test.espresso.matcher.ViewMatchers;
import androidx.test.espresso.util.HumanReadables;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

/* loaded from: classes.dex */
public final class GeneralClickAction implements ViewAction {
    private static final String TAG = "GeneralClickAction";
    private final int buttonState;
    final CoordinatesProvider coordinatesProvider;
    private final int inputDevice;
    final PrecisionDescriber precisionDescriber;
    private final Optional<ViewAction> rollbackAction;
    final Tapper tapper;

    @Deprecated
    public GeneralClickAction(Tapper tapper, CoordinatesProvider coordinatesProvider, PrecisionDescriber precisionDescriber) {
        this(tapper, coordinatesProvider, precisionDescriber, 0, 0, null);
    }

    public GeneralClickAction(Tapper tapper, CoordinatesProvider coordinatesProvider, PrecisionDescriber precisionDescriber, int inputDevice, int buttonState) {
        this(tapper, coordinatesProvider, precisionDescriber, inputDevice, buttonState, null);
    }

    @Deprecated
    public GeneralClickAction(Tapper tapper, CoordinatesProvider coordinatesProvider, PrecisionDescriber precisionDescriber, ViewAction rollbackAction) {
        this(tapper, coordinatesProvider, precisionDescriber, 0, 0, rollbackAction);
    }

    public GeneralClickAction(Tapper tapper, CoordinatesProvider coordinatesProvider, PrecisionDescriber precisionDescriber, int inputDevice, int buttonState, ViewAction rollbackAction) {
        this.coordinatesProvider = coordinatesProvider;
        this.tapper = tapper;
        this.precisionDescriber = precisionDescriber;
        this.inputDevice = inputDevice;
        this.buttonState = buttonState;
        this.rollbackAction = Optional.fromNullable(rollbackAction);
    }

    @Override // androidx.test.espresso.ViewAction
    public Matcher<View> getConstraints() {
        Matcher<View> isDisplayingAtLeast = ViewMatchers.isDisplayingAtLeast(90);
        return this.rollbackAction.isPresent() ? Matchers.allOf(isDisplayingAtLeast, this.rollbackAction.get().getConstraints()) : isDisplayingAtLeast;
    }

    @Override // androidx.test.espresso.ViewAction
    public void perform(UiController uiController, View view) {
        char c;
        float[] calculateCoordinates = this.coordinatesProvider.calculateCoordinates(view);
        float[] describePrecision = this.precisionDescriber.describePrecision();
        Tapper.Status status = Tapper.Status.FAILURE;
        int i = 0;
        while (status != Tapper.Status.SUCCESS && i < 3) {
            try {
                c = 3;
            } catch (RuntimeException e) {
                e = e;
                c = 3;
            }
            try {
                status = this.tapper.sendTap(uiController, calculateCoordinates, describePrecision, this.inputDevice, this.buttonState);
                if (Log.isLoggable(TAG, 3)) {
                    String valueOf = String.valueOf(String.format("%s - At Coordinates: %d, %d and precision: %d, %d", getDescription(), Integer.valueOf((int) calculateCoordinates[0]), Integer.valueOf((int) calculateCoordinates[1]), Integer.valueOf((int) describePrecision[0]), Integer.valueOf((int) describePrecision[1])));
                    Log.d(TAG, valueOf.length() != 0 ? "perform: ".concat(valueOf) : new String("perform: "));
                }
                int pressedStateDuration = ViewConfiguration.getPressedStateDuration();
                if (pressedStateDuration > 0) {
                    uiController.loopMainThreadForAtLeast(pressedStateDuration);
                }
                if (status == Tapper.Status.WARNING) {
                    if (!this.rollbackAction.isPresent()) {
                        break;
                    }
                    this.rollbackAction.get().perform(uiController, view);
                }
                i++;
            } catch (RuntimeException e2) {
                e = e2;
                PerformException.Builder builder = new PerformException.Builder();
                Object[] objArr = new Object[5];
                objArr[0] = getDescription();
                objArr[1] = Integer.valueOf((int) calculateCoordinates[0]);
                objArr[2] = Integer.valueOf((int) calculateCoordinates[1]);
                objArr[c] = Integer.valueOf((int) describePrecision[0]);
                objArr[4] = Integer.valueOf((int) describePrecision[1]);
                throw builder.withActionDescription(String.format("%s - At Coordinates: %d, %d and precision: %d, %d", objArr)).withViewDescription(HumanReadables.describe(view)).withCause(e).build();
            }
        }
        c = 3;
        if (status == Tapper.Status.FAILURE) {
            PerformException.Builder withViewDescription = new PerformException.Builder().withActionDescription(getDescription()).withViewDescription(HumanReadables.describe(view));
            Object[] objArr2 = new Object[9];
            objArr2[0] = Float.valueOf(calculateCoordinates[0]);
            objArr2[1] = Float.valueOf(calculateCoordinates[1]);
            objArr2[2] = Float.valueOf(describePrecision[0]);
            objArr2[c] = Float.valueOf(describePrecision[1]);
            objArr2[4] = this.tapper;
            objArr2[5] = this.coordinatesProvider;
            objArr2[6] = this.precisionDescriber;
            objArr2[7] = Integer.valueOf(i);
            objArr2[8] = Boolean.valueOf(this.rollbackAction.isPresent());
            throw withViewDescription.withCause(new RuntimeException(String.format("Couldn't click at: %s,%s precision: %s, %s . Tapper: %s coordinate provider: %s precision describer: %s. Tried %s times. With Rollback? %s", objArr2))).build();
        } else if (this.tapper == Tap.SINGLE && (view instanceof WebView)) {
            uiController.loopMainThreadForAtLeast(ViewConfiguration.getDoubleTapTimeout());
        }
    }

    @Override // androidx.test.espresso.ViewAction
    public String getDescription() {
        return String.valueOf(this.tapper.toString().toLowerCase()).concat(" click");
    }
}
